//
//  ZYClassCardWithdrawalSourceController.m
//  优学荟管理系统
//
//  Created by Ethan on 2017/9/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYClassCardWithdrawalSourceController.h"
#import "ClassCardWithdrawalSourceModel.h"
#import "ClassCardWithdrawalSourceCell.h"
#define kCell @"cell"
@interface ZYClassCardWithdrawalSourceController ()
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@end

@implementation ZYClassCardWithdrawalSourceController

- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}



#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"资金来源";
    [self.tableView registerClass:[ClassCardWithdrawalSourceCell  class] forCellReuseIdentifier:kCell];
    self.tableView.estimatedRowHeight = 200;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    self.tableView.tableFooterView = [UIView new];
    
    [self setUpRefresh];
    
}


#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}





- (void)setNetWork{
 
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=detailWithdrawClassCard&pageNumber=%ld&withdrawNo=%@",(long)self.page,self.orderId];
    [HUD showHUD];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [HUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            NSArray *arr = [t valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in arr) {
                
                ClassCardWithdrawalSourceModel *model = [ClassCardWithdrawalSourceModel  new];
                
                [model setValuesForKeysWithDictionary:dict];
                
                
                
                [self.dataSource addObject:model];
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
        
        
    } Fail:^(NSError *Error) {
        [HUD showHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
    
}




- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    self.back ? self.back ():nil;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ClassCardWithdrawalSourceCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    if (self.dataSource.count > indexPath.row) {
        
        ClassCardWithdrawalSourceModel *model = self.dataSource[indexPath.row];
        
        [cell setAssignZYSourceDetailsModel:model];
        
    }
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //补齐分割线、两边延长
    
    [self replenishLine:cell];
    
}



//补齐分割线、两边延长

-(void)replenishLine:(UIView *)view{
    
    if ([view respondsToSelector:@selector(setSeparatorInset:)]){
        
        [(UITableViewCell *)view setSeparatorInset:UIEdgeInsetsZero];//注意这里：必须将view强制转换成（UITableViewCell ＊）类，不然会报错。
        
    }
    
    if ([view respondsToSelector:@selector(setLayoutMargins:)]){
        
        [view setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

@end
